package top.lyc829.zzl_android.ui.team.add_team;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xuexiang.xui.widget.picker.XSeekBar;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.data.DateUtils;

import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Text;

import java.util.Date;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.logic.bean.ActivityBean;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.util.EditTextNumberFilter;
import top.lyc829.zzl_android.logic.util.Utils;

public class AddActivityAdapter extends BaseQuickAdapter<ActivityBean, BaseViewHolder> {

    public AddActivityAdapter() {
        super(R.layout.add_activity_item);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, ActivityBean activityBean) {

//        ((EditText)baseViewHolder.getView(R.id.add_activity_name)).getText().toString().trim();
        baseViewHolder.setText(R.id.txt_activity_sort, "活动" + (baseViewHolder.getAdapterPosition() + 1));
        EditText editText = baseViewHolder.getView(R.id.add_activity_name);

//        通过tag判断当前editText是否已经设置监听，有监听的话，移除监听再给editText赋值
        if (editText.getTag() instanceof TextWatcher){
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
//        必须在判断tag后给editText赋值，否则会数据错乱
        editText.setText(activityBean.getName());
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable)) {
                    activityBean.setName(editable.toString().trim());
                }
            }
        };
//        给item中的editText设置监听
        editText.addTextChangedListener(watcher);
//        给editText设置tag，以便于判断当前editText是否已经设置监听
        editText.setTag(watcher);

//        editText.setText(activityBean.getName());
//        editText.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s != null && !s.toString().equals(activityBean.getName())) {
//                    activityBean.setName(s.toString());
//                }
//            }
//        });

        baseViewHolder.setText(R.id.txt_base_name, activityBean.getBaseName());
        if (!StringUtils.isEmpty(activityBean.getBeginDate())) {
            baseViewHolder.setText(R.id.tv_start_date, DateUtils.millis2String(Long.parseLong(activityBean.getBeginDate()), DateUtils.yyyyMMdd.get()));
        } else {
            baseViewHolder.setText(R.id.tv_start_date, DateUtils.date2String(DateUtils.getStartOfDay(DateUtils.getNowDate()), DateUtils.yyyyMMdd.get()));
        }
        if (!StringUtils.isEmpty(activityBean.getEndDate())) {
            baseViewHolder.setText(R.id.tv_end_date, DateUtils.millis2String(Long.parseLong(activityBean.getEndDate()), DateUtils.yyyyMMdd.get()));
        } else {
            baseViewHolder.setText(R.id.tv_end_date, DateUtils.date2String(DateUtils.getEndOfDay(DateUtils.getNowDate()), DateUtils.yyyyMMdd.get()));
        }

//        XSeekBar seekBar = baseViewHolder.getView(R.id.people_number);
//
//        if (activityBean.getNumber() == null || activityBean.getNumber().equals(0)) {
//            seekBar.setDefaultValue(10);
//        } else {
//            seekBar.setDefaultValue(activityBean.getNumber());
//        }
//        seekBar.setOnSeekBarListener((seekBar12, newValue) -> {
//            activityBean.setNumber(newValue);
//            Utils.Sout(String.valueOf(newValue));
//        });

        EditText editTextNumber = baseViewHolder.getView(R.id.people_number);


//        通过tag判断当前editText是否已经设置监听，有监听的话，移除监听再给editText赋值
        if (editTextNumber.getTag() instanceof TextWatcher){
            editTextNumber.removeTextChangedListener((TextWatcher) editTextNumber.getTag());
        }
//        必须在判断tag后给editText赋值，否则会数据错乱
        editTextNumber.setText(activityBean.getNumber() == null ? "" : String.valueOf(activityBean.getNumber()));
        TextWatcher watcher1 = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable)) {
                    activityBean.setNumber(Integer.parseInt(editable.toString().trim()));
                }
            }
        };
//        给item中的editText设置监听
        editTextNumber.addTextChangedListener(watcher1);
//        给editText设置tag，以便于判断当前editText是否已经设置监听
        editTextNumber.setTag(watcher1);
        editTextNumber.setFilters(new InputFilter[]{new EditTextNumberFilter()});

        //baseViewHolder.setIsRecyclable(false);

    }




}
